//
//  DataAnalyzerTester.m
//  MarketDataAnalyzer
//
//  Created by jiro on 10/01/23.
//  Copyright 2010 jiro music. All rights reserved.
//

#import "DataAnalyzerTester.h"


@implementation DataAnalyzerTester


- (id) init
{
	self = [ super init ];

	NSMutableDictionary* stockExchangeJaTableFields = [ [ NSMutableDictionary alloc ] init ];
	[ stockExchangeJaTableFields
		setObject: [ [ FieldInfo alloc ] initWithDbDataType: DbData_String andFieldName: kText_BrandCode ]
		forKey: [ NSNumber numberWithInt: Field_BrandCode ] ];
	[ stockExchangeJaTableFields
		setObject: [ [ FieldInfo alloc ] initWithDbDataType: DbData_Date andFieldName: kText_ExchangeDate ]
		forKey: [ NSNumber numberWithInt: Field_ExchangeDate ] ];
	[ stockExchangeJaTableFields
		setObject: [ [ FieldInfo alloc ] initWithDbDataType: DbData_Money andFieldName: kText_StartPrice ]
		forKey: [ NSNumber numberWithInt: Field_StartPrice ] ];
	[ stockExchangeJaTableFields
		setObject: [ [ FieldInfo alloc ] initWithDbDataType: DbData_Money andFieldName: kText_EndPrice ]
		forKey: [ NSNumber numberWithInt: Field_EndPrice ] ];
	[ stockExchangeJaTableFields
		setObject: [ [ FieldInfo alloc ] initWithDbDataType: DbData_Money andFieldName: kText_HighestPrice ]
		forKey: [ NSNumber numberWithInt: Field_HighestPrice ] ];
	[ stockExchangeJaTableFields
		setObject: [ [ FieldInfo alloc ] initWithDbDataType: DbData_Money andFieldName: kText_LowestPrice ]
		forKey: [ NSNumber numberWithInt: Field_LowestPrice ] ];
	[ stockExchangeJaTableFields
		setObject: [ [ FieldInfo alloc ] initWithDbDataType: DbData_Real andFieldName: kText_Volume ]
		forKey: [ NSNumber numberWithInt: Field_Volume ] ];
	[ stockExchangeJaTableFields
		setObject: [ [ FieldInfo alloc ] initWithDbDataType: DbData_Real andFieldName: kText_AdjustRate ]
		forKey: [ NSNumber numberWithInt: Field_AdjustRate ] ];
	TableInfo* stockExchangeJaTable =
		[ [ TableInfo alloc ] initWithTableName: kText_StockExchangeJaTable andFields: stockExchangeJaTableFields ];

	NSMutableDictionary* stockUnitDivitionJaTableFields = [ [ NSMutableDictionary alloc ] init ];
	[ stockUnitDivitionJaTableFields
		setObject: [ [ FieldInfo alloc ] initWithDbDataType: DbData_String andFieldName: kText_BrandCode ]
		forKey: [ NSNumber numberWithInt: Field_BrandCode ] ];
	[ stockUnitDivitionJaTableFields
		setObject: [ [ FieldInfo alloc ] initWithDbDataType: DbData_Date andFieldName: kText_DivisionDate ]
		forKey: [ NSNumber numberWithInt: Field_DivisionDate ] ];
	[ stockUnitDivitionJaTableFields
		setObject: [ [ FieldInfo alloc ] initWithDbDataType: DbData_Real andFieldName: kText_UnitDivided ]
		forKey: [ NSNumber numberWithInt: Field_UnitDivided ] ];
	TableInfo* stockUnitDivitionTable =
		[ [ TableInfo alloc ] initWithTableName: kText_StockUnitDivisionJaTable andFields: stockUnitDivitionJaTableFields ];

	NSMutableDictionary* talbes = [ [ NSMutableDictionary alloc ] init ];
	[ talbes setObject: stockExchangeJaTable	forKey: [ [ NSNumber alloc ] initWithInt: Table_StockExchangeJaTable ] ];
	[ talbes setObject: stockUnitDivitionTable	forKey: [ [ NSNumber alloc ] initWithInt: Table_StockUnitDvisionTable ] ];

	DbInfo* dbInfo = [ [ DbInfo alloc ] initWithDbName: kText_MarketDatabase andTables: talbes ];
	analyzer = [ [ DataAnalyzer alloc ] initWithConnectionToDb: @"MarketDatabase" ofHost: @"localhost" asUser: @"jiro" andDbInfo: dbInfo ];
	if ( analyzer == nil )
	{
		return nil;
	}

	return self;
}

- (void) dealloc
{
	[ analyzer dealloc ];
	[ super dealloc ];
}

- (void) testAll
{
	[ self test_AverageOf ];

	[ self test_VarianceOf ];

	[ self test_StandardDeviationOf ];

	[ self test_CovarianceOf ];

	[ self test_CorrelationCoefficientOf ];

	[ self test_RegressionCoefficientOf ];

	[ self test_IntersectionOf ];

}

- (void) test_AverageOf
{
	DataAnalyzeArg* arg = [ [ DataAnalyzeArg alloc ] init ];
	[ arg setTableId: Table_StockExchangeJaTable ];
	[ arg setDataFieldId: Field_StartPrice ];
	[ arg setConditionFieldId: Field_BrandCode ];
	[ arg setCondition: @"1301" ];
	[ arg setRangeConditionFieldId: Field_ExchangeDate ];
	[ arg setFromCondition: @"2010-1-1" ];
	[ arg setToCondition: @"2010-1-15" ];

	double value;
	[ analyzer averageOf: arg intoValue: & value ];
	NSAssert1( ( round( 1000000 * 188.666666666667 ) ==  round( 1000000 * value ) ), @"must be 188.66667, but %f", value ); 
}

- (void) test_VarianceOf
{
	DataAnalyzeArg* arg = [ [ DataAnalyzeArg alloc ] init ];
	[ arg setTableId: Table_StockExchangeJaTable ];
	[ arg setDataFieldId: Field_StartPrice ];
	[ arg setConditionFieldId: Field_BrandCode ];
	[ arg setCondition: @"1301" ];
	[ arg setRangeConditionFieldId: Field_ExchangeDate ];
	[ arg setFromCondition: @"2010-1-1" ];
	[ arg setToCondition: @"2010-1-15" ];

	double value;
	[ analyzer varianceOf: arg intoValue: & value ];
	NSAssert1( ( round( 1000000 * 3.5 ) ==  round( 1000000 * value ) ), @"must be 3.5, but %f", value ); 
}

- (void) test_StandardDeviationOf
{
	DataAnalyzeArg* arg = [ [ DataAnalyzeArg alloc ] init ];
	[ arg setTableId: Table_StockExchangeJaTable ];
	[ arg setDataFieldId: Field_StartPrice ];
	[ arg setConditionFieldId: Field_BrandCode ];
	[ arg setCondition: @"1301" ];
	[ arg setRangeConditionFieldId: Field_ExchangeDate ];
	[ arg setFromCondition: @"2010-1-1" ];
	[ arg setToCondition: @"2010-1-15" ];

	double value;
	[ analyzer standardDeviationOf: arg intoValue: & value ];
	NSAssert1( ( round( 1000000 * 1.87082869338697 ) ==  round( 1000000 * value ) ), @"must be 1.87082869338697, but %f", value ); 
}

- (void) test_CovarianceOf
{
	DataAnalyzeArg* arg1 = [ [ DataAnalyzeArg alloc ] init ];
	[ arg1 setTableId: Table_StockExchangeJaTable ];
	[ arg1 setDataFieldId: Field_StartPrice ];
	[ arg1 setConditionFieldId: Field_BrandCode ];
	[ arg1 setCondition: @"1301" ];
	[ arg1 setRangeConditionFieldId: Field_ExchangeDate ];
	[ arg1 setFromCondition: @"2010-1-1" ];
	[ arg1 setToCondition: @"2010-1-15" ];

	DataAnalyzeArg* arg2 = [ [ DataAnalyzeArg alloc ] init ];
	[ arg2 setTableId: Table_StockExchangeJaTable ];
	[ arg2 setDataFieldId: Field_StartPrice ];
	[ arg2 setConditionFieldId: Field_BrandCode ];
	[ arg2 setCondition: @"1305" ];
	[ arg2 setRangeConditionFieldId: Field_ExchangeDate ];
	[ arg2 setFromCondition: @"2010-1-1" ];
	[ arg2 setToCondition: @"2010-1-15" ];
	
	double value;
	[ analyzer covarianceOf: arg1 arg: arg2 andJoinFieldId: Field_ExchangeDate intoValue: & value ];
//	NSLog( @"%@", [ analyzer errorMessage ] );
	NSAssert1( ( round( 1000000 *  22.0416666666667 ) ==  round( 1000000 * value ) ), @"must be 22.0416666666667, but %f", value );
}

- (void) test_CorrelationCoefficientOf
{
	DataAnalyzeArg* arg1 = [ [ DataAnalyzeArg alloc ] init ];
	[ arg1 setTableId: Table_StockExchangeJaTable ];
	[ arg1 setDataFieldId: Field_StartPrice ];
	[ arg1 setConditionFieldId: Field_BrandCode ];
	[ arg1 setCondition: @"1301" ];
	[ arg1 setRangeConditionFieldId: Field_ExchangeDate ];
	[ arg1 setFromCondition: @"2010-1-1" ];
	[ arg1 setToCondition: @"2010-1-15" ];

	DataAnalyzeArg* arg2 = [ [ DataAnalyzeArg alloc ] init ];
	[ arg2 setTableId: Table_StockExchangeJaTable ];
	[ arg2 setDataFieldId: Field_StartPrice ];
	[ arg2 setConditionFieldId: Field_BrandCode ];
	[ arg2 setCondition: @"1305" ];
	[ arg2 setRangeConditionFieldId: Field_ExchangeDate ];
	[ arg2 setFromCondition: @"2010-1-1" ];
	[ arg2 setToCondition: @"2010-1-15" ];
	
	double value;
	[ analyzer correlationCoefficientOf: arg1 arg: arg2 andJoinFieldId: Field_ExchangeDate intoValue: & value ];
	NSAssert1( ( round( 1000000 * 0.812320998924219 ) ==  round( 1000000 * value ) ), @"must be 0.812320998924219, but %f", value );
}

- (void) test_RegressionCoefficientOf
{
	DataAnalyzeArg* arg1 = [ [ DataAnalyzeArg alloc ] init ];
	[ arg1 setTableId: Table_StockExchangeJaTable ];
	[ arg1 setDataFieldId: Field_StartPrice ];
	[ arg1 setConditionFieldId: Field_BrandCode ];
	[ arg1 setCondition: @"1301" ];
	[ arg1 setRangeConditionFieldId: Field_ExchangeDate ];
	[ arg1 setFromCondition: @"2010-1-1" ];
	[ arg1 setToCondition: @"2010-1-15" ];

	DataAnalyzeArg* arg2 = [ [ DataAnalyzeArg alloc ] init ];
	[ arg2 setTableId: Table_StockExchangeJaTable ];
	[ arg2 setDataFieldId: Field_StartPrice ];
	[ arg2 setConditionFieldId: Field_BrandCode ];
	[ arg2 setCondition: @"1305" ];
	[ arg2 setRangeConditionFieldId: Field_ExchangeDate ];
	[ arg2 setFromCondition: @"2010-1-1" ];
	[ arg2 setToCondition: @"2010-1-15" ];

	double covariance;
	double variance;
	NSAssert( [ analyzer covarianceOf: arg1 arg: arg2 andJoinFieldId: Field_ExchangeDate intoValue: & covariance ], @"covarianceOf fail" );
	NSAssert( [ analyzer varianceOf: arg1 intoValue: & variance ], @"varianceOf fail" );
	NSAssert1( ( round( 1000000 * 22.0416666666667 ) ==  round( 1000000 * covariance ) ), @"must be 22.0416666666667, but %f", covariance );
	NSAssert1( ( round( 1000000 * 3.5 ) ==  round( 1000000 * variance ) ), @"must be 3.5, but %f", variance );

	double value;
	double regressionCoefficient = covariance / variance;
	NSAssert( [ analyzer regressionCoefficientOf: arg1 arg: arg2 andJoinFieldId: Field_ExchangeDate intoValue: & value ], @"regressionCoefficientOf fail" );
	NSAssert2( ( round( 1000000 *  regressionCoefficient ) ==  round( 1000000 * value ) ), @"must be %f, but %f", regressionCoefficient, value );
}

- (void) test_IntersectionOf
{
	DataAnalyzeArg* arg1 = [ [ DataAnalyzeArg alloc ] init ];
	[ arg1 setTableId: Table_StockExchangeJaTable ];
	[ arg1 setDataFieldId: Field_StartPrice ];
	[ arg1 setConditionFieldId: Field_BrandCode ];
	[ arg1 setCondition: @"1301" ];
	[ arg1 setRangeConditionFieldId: Field_ExchangeDate ];
	[ arg1 setFromCondition: @"2010-1-1" ];
	[ arg1 setToCondition: @"2010-1-15" ];

	DataAnalyzeArg* arg2 = [ [ DataAnalyzeArg alloc ] init ];
	[ arg2 setTableId: Table_StockExchangeJaTable ];
	[ arg2 setDataFieldId: Field_StartPrice ];
	[ arg2 setConditionFieldId: Field_BrandCode ];
	[ arg2 setCondition: @"1305" ];
	[ arg2 setRangeConditionFieldId: Field_ExchangeDate ];
	[ arg2 setFromCondition: @"2010-1-1" ];
	[ arg2 setToCondition: @"2010-1-15" ];
	
	double regressionCoefficient;
	double average1;
	double average2;
	NSAssert( [ analyzer regressionCoefficientOf: arg1 arg: arg2 andJoinFieldId: Field_ExchangeDate intoValue: & regressionCoefficient ],
			  @"regressionCoefficient fail" );
	NSAssert( [ analyzer averageOf: arg1 intoValue: & average1 ], @"average arg1 fail" );
	NSAssert( [ analyzer averageOf: arg2 intoValue: & average2 ], @"average arg2 fail" );

	double intersection = average2 - regressionCoefficient * average1;
	double value;
	[ analyzer intersectionOf: arg1 arg: arg2 andJoinFieldId: Field_ExchangeDate intoValue: & value ];
	NSAssert2( ( round( 1000000 *  intersection ) ==  round( 1000000 * value ) ), @"must be %f, but %f", intersection, value );
}

@end
